package com.example.intent_demo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	

	EditText val1;
	EditText val2;
	Button addButton;
	TextView lblResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		val1 = (EditText) findViewById(R.id.editText1);
		val2 = (EditText) findViewById(R.id.editText2);
		addButton = (Button) findViewById(R.id.button1);
		lblResult = (TextView) findViewById(R.id.textView1);
		addButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Double d1 = Double.parseDouble(val1.getText().toString());
				Double d2 = Double.parseDouble(val2.getText().toString());
				
				Intent myIntentA1A2 = new Intent(MainActivity.this, Activity2.class);
				
				// create a container to ship data
				Bundle myData = new Bundle();
				// add <key,value> data items to the container
				myData.putDouble("val1", d1); myData.putDouble("val2", d2);
				// attach the container to the intent
				myIntentA1A2.putExtras(myData);
				
				startActivityForResult(myIntentA1A2, 101);
				
			}
		});
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
			try{
				if ((requestCode == 101 ) && (resultCode == Activity.RESULT_OK)){ 
					Bundle myResults = data.getExtras();
					Double vresult = myResults.getDouble("vresult"); lblResult.setText("Sum is " + vresult);
				}
			}
			catch (Exception e) {
			lblResult.setText("Oops! - " + requestCode + " " + resultCode);
			}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
